package com.three.iot.rest.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.three.iot.api.DeviceService;
import com.three.iot.api.UserDeviceService;
import com.three.iot.commons.dto.DeviceCondDto;
import com.three.iot.commons.dto.DeviceRespDto;
import com.three.iot.commons.enums.Result;
import com.three.iot.commons.enums.ResultCode;
import com.three.iot.commons.po.UserInfo;
import com.three.iot.rest.controller.common.AbstractDeviceAttrController;
import com.three.iot.rest.utils.RequestUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author luiz
 * @Title: DeviceController
 * @ProjectName iot-server
 * @Description: TODO
 * @date 2019-05-14 14:45
 */
@RestController
@RequestMapping("/admin/device")
@Api(tags = "设备信息api")
public class DeviceController extends AbstractDeviceAttrController {
    private static final Logger logger = LoggerFactory.getLogger(DeviceController.class);
    @Resource
    private DeviceService deviceService;
    @Resource
    private UserDeviceService userDeviceService;

    /**
     * 设备分页查询
     */
    @PostMapping("/listPage")
    @ApiOperation(value = "设备分页查询", notes = "设备分页查询")
    public Result findPage(@RequestBody DeviceCondDto deviceCondDto) {
        try {
            Page<DeviceRespDto> deviceRespDtoPage = deviceService.listPage(deviceCondDto);
            PageInfo pageInfo = new PageInfo(deviceRespDtoPage);
            return Result.newSuccess(pageInfo);
        } catch (Exception e) {
            logger.error("设备分页查询失败:{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    /**
     * 设备模糊查询
     */
    @GetMapping("/listLike/{condStr}")
    @ApiOperation(value = "设备模糊查询", notes = "设备模糊查询")
    public Result listLike(@PathVariable("condStr")String condStr) {
        try {
            List<DeviceRespDto> deviceRespDtoList = deviceService.listLike(condStr);
            return Result.newSuccess(deviceRespDtoList);
        } catch (Exception e) {
            logger.error("设备分页查询失败:{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    /**
     * 设备刷新
     */
    @PostMapping("/refresh")
    @ApiOperation(value = "设备刷新", notes = "设备刷新")
    public Result refresh(HttpServletRequest request) {
        try {
             UserInfo userInfo=RequestUtils.getCurrentUser(request);
             logger.info("设备刷新，操作人:{}",userInfo);
             deviceService.refresh(userInfo);
            return Result.newSuccess();
        } catch (Exception e) {
            logger.error("设备刷新失败:{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    /**
     * 根据设备编号查询用户列表
     */
    @PostMapping("/findUserByIotId/{iotId}")
    @ApiOperation(value = "根据设备编号查询用户列表", notes = "根据设备编号查询用户列表")
    public Result findUserByIotId(@PathVariable("iotId") String iotId) {
        try {
            return userDeviceService.findUserByIotId(iotId);
        } catch (Exception e) {
            logger.error("根据设备编号查询用户列表:{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    /**
     * 根据设备编号查询属性列表
     */
    @PostMapping("/findAttrByIotId/{iotId}")
    @ApiOperation(value = "根据设备编号查询属性列表", notes = "根据设备编号查询属性列表")
    public Result findAttrByIotId(@PathVariable("iotId") String iotId) {
        try {
            return deviceService.findAttrByIotId(iotId);
        } catch (Exception e) {
            logger.error("根据设备编号查询属性列表:{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    /**
     * 绑定用户
     */
    @PostMapping("/bindUser/{iotId}/{userNo}")
    @ApiOperation(value = "绑定用户", notes = "绑定用户")
    public Result bindUser(@PathVariable("iotId") String iotId,@PathVariable("userNo") String userNo,HttpServletRequest request) {
        try {
            String operUserNo=RequestUtils.getCurrentUserNo(request);
            return userDeviceService.bindUser(iotId,userNo,operUserNo);
        } catch (Exception e) {
            logger.error("绑定用户:{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    /**
     * 解绑用户
     */
    @PostMapping("/unbind/{iotId}/{userNo}")
    @ApiOperation(value = "解绑用户", notes = "解绑用户")
    public Result unbind(@PathVariable("iotId") String iotId,@PathVariable("userNo") String userNo,HttpServletRequest request) {
        try {
            String operUserNo=RequestUtils.getCurrentUserNo(request);
            return userDeviceService.unbind(iotId,userNo,operUserNo);
        } catch (Exception e) {
            logger.error("解绑用户:{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }



}
